package com.jjy.admin.controller.job;

import cn.hutool.json.JSONUtil;
import com.jjy.admin.domain.TaskJob;
import com.jjy.admin.service.common.CommonService;
import com.jjy.admin.service.job.impl.JobServiceImpl;
import com.jjy.common.annotation.ApiResource;
import com.jjy.common.domain.PageResult;
import com.jjy.common.domain.R;
import com.jjy.common.enums.ResBizTypeEnum;
import com.jjy.common.utils.StringUtils;
import com.jjy.admin.domain.JobEntity;
import com.jjy.admin.domain.JobSpiderDTO;
import com.jjy.admin.service.job.JobService;
import org.openqa.selenium.json.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统功能
 *
 * @author jjy
 */
@RestController
@RequestMapping("/sysFunctions")
@ApiResource(name = "岗位查询", resBizType = ResBizTypeEnum.SYSTEM)
public class JobController {
    @Autowired
    private JobService jobService;
    @Autowired
    private CommonService commonService;

    private static final Logger log = LoggerFactory.getLogger(  JobController.class);

    /**
     * 分页
     */
    @GetMapping("/jobViews/list")
    @PreAuthorize("@ss.hasPermi('sysFunctions:jobViews:list')")
    public R page(JobEntity jobEntity) {
        if (StringUtils.isNotEmpty(jobEntity.getProvince())&&jobEntity.getProvince().equals("热门地区")){
            jobEntity.setProvince(null);
        }

        PageResult<JobEntity> page = jobService.page(jobEntity);

        return R.ok().put(page);
    }


    /**
     * 导出
     */
    @GetMapping("/jobViews/exportList")
    @PreAuthorize("@ss.hasPermi('sysFunctions:jobViews:exportList')")
    public R exportList(JobEntity jobEntity) {
        if (StringUtils.isNotEmpty(jobEntity.getProvince())&&jobEntity.getProvince().equals("热门地区")){
            jobEntity.setProvince(null);
        }

        List<JobEntity> jobEntities = jobService.exportList(jobEntity);
        return R.ok().put("data", jobEntities);

    }

    /**
     * 爬取job
     */
    @GetMapping("/jobSpider/doSpider")
    @PreAuthorize("@ss.hasPermi('sysFunctions:jobSpider:doSpider')")
    public R doSpider( JobSpiderDTO jobSpider) {
        int i = jobService.doSpider(jobSpider);
        log.info(  "爬取数量："+i);
        if (i>0){
            return R.ok("数据爬取成功！",0);
        }else {
            return R.error( 0,"数据爬取失败！");
        }
    }


    /**
     * 定时爬取job
     */
    @GetMapping("/jobSpider/doAynSpider")
    @PreAuthorize("@ss.hasPermi('sysFunctions:jobSpider:doAynSpider')")
    public R doAynSpider( JobSpiderDTO jobSpider) {

        TaskJob taskJob = new TaskJob();
        taskJob.setTaskType(1);
        taskJob.setTaskName("定时爬取job");
        taskJob.setTaskCondition(JSONUtil.toJsonStr(jobSpider));
        int i = commonService.insertTaskJob(taskJob);

        if (i>0){
            return R.ok("定时爬取任务创建成功！请10分钟后进行查看！",0);
        }else {
            return R.error( "定时爬取任务创建失败！");
        }
    }
}